/*
Output: Table 3. Differences in Subgroup and Population Means
*/


run "/Users/holyfantastic/Dropbox/Forest/Paper/Nature/FinalDocuments/Code/000declare_path" // change to your local path


**# Housekeeping and clean data 

use "ParcelLevelDataSept282023_VerNov82024_R1.dta" , clear

replace Landtr_Cut_mean_forest = Landtr_Cut_mean_forest *100

replace YearsOwned =. if YearsOwned < 0
replace dis_road = dis_road * 1000
label var YearsOwned ""

**## Table mean diff - Appendix

cap drop _*
xi: sum i.forestHaGroup i.roadGroup i.yearGroup
gen _IforestHaG_1 = 1 - _IforestHaG_2 - _IforestHaG_3 - _IforestHaG_4
gen _IroadGroup_1 = 1 - _IroadGroup_2 - _IroadGroup_3 - _IroadGroup_4
gen _IyearGroup_1 = 1 - _IyearGroup_2 - _IyearGroup_3 - _IyearGroup_4

global vars1  ndvi1985  dndvi_1985_2019  Landtr_Cut_mean_forest Landtr_Cut_mean_forest8599 Landtr_Cut_mean_forest0014 Landtr_Cut_mean_forest1519 forestHaGroup _IforestHaG_1 _IforestHaG_2 _IforestHaG_3 _IforestHaG_4 roadGroup _IroadGroup_1 _IroadGroup_2 _IroadGroup_3 _IroadGroup_4 yearGroup _IyearGroup_1 _IyearGroup_2 _IyearGroup_3 _IyearGroup_4 iPerson iLocal
//
// global vars2
//   i.forestHaGroup i.roadGroup   i.yearGroup i.iPerson  i.iLocal 
//		
		
matrix drop _all

* first three columns

mat A = J(30,6,.)

local i = 1
foreach var in $vars1{
	
	dis "`var'"
	
	foreach gr in 1 2 3{
	
		local group = `gr'  

		qui{
		* first three columns 
		sum `var'
		local pop_mean = r(mean)
		local pop_sd = r(sd)
		sum `var' if group == `group'
		local group_mean = r(mean)
		local mean_diff_std = (`group_mean' - `pop_mean')/`pop_sd'
		local group_n = r(N)
		local df = `group_n' - 1
		mat A[`i',`group'] = `mean_diff_std'
		mat list A
		
		* second three columns 
// 		ttest `var' == `pop_mean' if group == `group'
		/*Nov 7, 2024*/
		local t_stat = (`group_mean' - `pop_mean') / (`pop_sd'/sqrt(`group_n'))
		local t_stat_abs = abs(`t_stat')
		dis "`group_n'"
		dis "`t_stat'"

		local p = 2 * ttail(`df',`t_stat_abs')
		dis "`p'"

		mat A[`i',`group'+3] = `p'
		}

	}
	
	local i = `i' + 1
}


matrix colnames A = "Declined" "In Process" "Enrolled" "Declined" "In Process" "Enrolled"
// matrix rownames A = "Normalized Vegetation Index (NDVI), 1985" "Change in NDVI, 1985-2019" "Average annual percent forested area cut, 1985-2019" "Average annual percent forested area cut, 1985-1999" "Average annual percent forested area cut, 2000-2014" "Average annual percent forested area cut, 2015-2019" "Forest acres" "Forest acres: Less than 10 acres" "Forest acres: 10-49 acres" "Forest acres: 50-199 acres" "Forest acres: 200 or more acres" "Distance to road, meters" "Distance to road: Less than 100 meters" "Distance to road: 100-249 meters" "Distance to road: 250-500 acres" "Distance to road: 500 or more meters" "Years owned" "Years owned: Less than 5 years" "Years owned: 5-9 years" "Years owned: 10-14 years" "Years owned: 15 or more years" "Owner is a person, not entity" "Owner's address is within 40 km of parcel"

mat list A, format(%9.3f) 

esttab matrix(A,fmt(3)) using "$table/table_3_mean_diff_p_R1_v2.csv", replace 
/*Generating table_3_mean_diff_p_R1_v2.tex from table_3_mean_diff_p_R1_v2.csv is done mannually*/

